// pages/cart/cart.js
Page({
  data: {
    carts:[],
    bool:false,
    isAllSelect:false,
    totalMoney:0,
   
    // 商品详情介绍
    carts: [
      {
        pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148058328876.jpg",
        name:"日本资生堂洗颜",
        price:200,
        isSelect:false,
        num: 1,
        // 数据设定
        count: {
          quantity: 2,
          min: 1,
          max: 20
        },
      },
      {
        pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058301941.jpg',
        name: "倩碧焕妍活力精华露",
        price: 340,
        isSelect: false,
        num: 1,
        // 数据设定
        count: {
          quantity: 1,
          min: 1,
          max: 20
        },
      },
      {
        pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/14805828016.jpg',
        name: "特效润肤露",
        price: 390,
        isSelect: false,
        num: 1,
        // 数据设定
        count: {
          quantity: 3,
          min: 1,
          max: 20
        },
      },
      {
        pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058228431.jpg',
        name: "倩碧水嫩保湿精华面霜",
        price: 490,
        isSelect: false,
        num:1,
        // 数据设定
        count: {
          quantity: 1,
          min: 1,
          max: 20
        },
      },
      {
        pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148057953326.jpg',
        name: "兰蔻清莹柔肤爽肤水",
        price: 289,
        isSelect: false,
        num: 1,
        // 数据设定
        count: {
          quantity: 10,
          min: 1,
          max: 20
        },
      },
      {
        pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148057921620_middle.jpg",
        name: "LANCOME兰蔻小黑瓶精华",
        price: 230,
        isSelect: false,
        num: 1,
        // 数据设定
        count: {
          quantity: 1,
          min: 1,
          max: 20
        },
      },
    ],
  },

  //勾选事件处理函数  
  switchSelect: function (e) {
    // 获取item项的id，和数组的下标值  
    var Allprice = 0,i=0;
    let id = e.target.dataset.id,

    index = parseInt(e.target.dataset.index);
    this.data.carts[index].isSelect = !this.data.carts[index].isSelect;
    //价钱统计
    if (this.data.carts[index].isSelect) {
      this.data.totalMoney = this.data.totalMoney + this.data.carts[index].price;
    }
    else {
      this.data.totalMoney = this.data.totalMoney - this.data.carts[index].price;
    }
   //是否全选判断
    for (i = 0; i < this.data.carts.length; i++) {
      Allprice = Allprice + this.data.carts[i].price;
    }
    if (Allprice == this.data.totalMoney)
    {
      this.data.isAllSelect=true;
    }
    else 
    {
      this.data.isAllSelect = false;
    }
    this.setData({
      carts: this.data.carts,
      totalMoney: this.data.totalMoney,
      isAllSelect: this.data.isAllSelect,
    })
  },
  //全选
  allSelect: function (e) {
    
    //处理全选逻辑
    // let i = 0;
    // if (!this.data.isAllSelect) {
    //   for (i = 0; i < this.data.carts.length; i++) {
    //     this.data.carts[i].isSelect = true;
    //     this.data.totalMoney = this.data.totalMoney + this.data.carts[i].price;
    //   }
    // }
    // else {
    //   for (i = 0; i < this.data.carts.length; i++) {
    //     this.data.carts[i].isSelect = false;
    //   }
    //   this.data.totalMoney = 0;
    // }
    for (let i = 0; i < this.data.carts.length; i++) {
      this.data.carts[i].isSelect = !this.data.carts[i].isSelect;
       //  this.data.totalMoney = this.data.totalMoney + this.data.carts[i].price;
    }
    let carts = this.data.carts
    let hehe=this
    this.setData({
      carts:carts
      
    })
    this.zong()
  },
  addCount(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].num;
    num = num + 1;
    carts[index].num = num;
    this.setData({
      carts: carts
    });
    // this.switchSelect();
    this.zong()
  },
  // 减少数量
  minusCount(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let num = carts[index].num;
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    carts[index].num = num;
    this.setData({
      carts: carts
    });
    // this.switchSelect();
    this.zong()
  },
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    carts.splice(index,1);              // 删除购物车列表里这个商品
    this.setData({
        carts: carts
    });
    if(!carts.length){                  // 如果购物车为空
        this.setData({
            hasList: false              // 修改标识为false，显示购物车为空页面
        });
    }else{                              // 如果不为空
      this.zong();           // 重新计算总价格
    }   
},

  // 去结算
  toBuy() {
    wx.showToast({
      title: '去结算',
      icon: 'success',
      duration: 3000
    });
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  //数量变化处理
  handleQuantityChange(e) {
    var componentId = e.componentId;
    var quantity = e.quantity;
    this.data.carts[componentId].count.quantity = quantity;
    this.setData({
      carts: this.data.carts,
    });
  },
  onload:function(){
    console.log(1)
  },
zong:function(){
  let qian = 0
  let carts = this.data.carts;
  for (let i = 0; i < this.data.carts.length; i++) {
    if (this.data.carts[i].isSelect){
      qian += carts[i].price * carts[i].num
    }
    //  this.data.totalMoney = this.data.totalMoney + this.data.carts[i].price;
  }
  this.setData({
    totalMoney: qian
  })

}
});